<?php

/**
 * MVM_MALL 网上商店系统  支付插件管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-14 $
 * $Id: payment.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if ($action=='list') {
    $list = get_dirinfo(MVMMALL_ROOT.'./include/payment','.class.php');
    for ($i = 0; $i < count($list); $i++) {
        require MVMMALL_ROOT.'./include/payment/'.$list[$i];
        $name = dhtmlchars(str_replace('.class.php','',$list[$i]));
        $pay  = $db->get_one("SELECT id,class_name FROM `{$tablepre}payment_table` WHERE class_name='$name'");
        if($pay['class_name']){
            $payment[$pay['class_name']]['install'] = 1;
            $payment[$pay['class_name']]['id']      = $pay['id'];
            $payment[$pay['class_name']]['edit']    = "admincp.php?module=$module&action=edit&uid=$pay[id]";
            $payment[$pay['class_name']]['del']     = "admincp.php?module=$module&action=del&uid=$pay[id]";
        }
        $payment[$name]['insert']                   = "admincp.php?module=$module&action=add&name=$name";
    }
    require template('payment');
    footer();
} elseif ($action=='add' && isset($name)) {
    if($setp==1 && $_POST){
        $pay = $db->get_one("SELECT id,class_name FROM `{$tablepre}payment_table` WHERE class_name='$name'");
        if($pay['class_name']){
            admin_msg('支付方式已经存在');
        } else {
            if(!file_exists(MVMMALL_ROOT.'./include/payment/'.$name.'.class.php')){
                admin_msg('该支付方式不存在');
            } else {
                require_once  MVMMALL_ROOT.'./include/payment/'.$name.'.class.php';
            }
            $config = array();
            foreach ($payment[$name]['cfg'] AS $key => $val)
            {
                $config[$key]['name']   = $val['name'];
                $config[$key]['value']  = $$val['name'];
                $config[$key]['label']  = $val['label'];
            }
            $config = serialize($config);
            $query = "INSERT INTO `{$tablepre}payment_table` SET
                  class_name  = '$name',
                  name        =  '$pay_name',
                  pay_desc    =  '$pay_desc',
                  online      = '$payment[$name][online]',
                  cfg         = '$config'
                 ";
            $db->query($query);
        }
        payment_list('update');
        admin_msg('success','admincp.php?module=payment&action=list');
    } else {
        $name   = dhtmlchars($name);
        require_once MVMMALL_ROOT.'./include/payment/'.$name.'.class.php';
        $fields = array();
        foreach ($payment[$name]['cfg'] AS $key => $val)
        {
            $fields[$key]['name']   = $val['name'];
            $fields[$key]['value']  = $val['value'];
            $fields[$key]['label']  = $val['label'];
        }
        /**$count = count($fields);
        * $fields[$count]['name']     = "pay_fee";
        $fields[$count]['value']    = $payment[$name]['pay_fee'];
        $fields[$count]['label']    = '支付手续费';
        **/
        @extract($payment[$name],EXTR_OVERWRITE);
        require template('payment_add');
        footer();
    }
} elseif ($action=='edit' && is_numeric($uid)) {
    if($setp==1 && $_POST){
        $pay      = $db->get_one("SELECT id,class_name FROM `{$tablepre}payment_table` WHERE id='$uid'");
        $name     = $pay['class_name'];
        $pay_name = dhtmlchars($pay_name);
        $nums = $db->counter("{$tablepre}payment_table","where  name='$pay_name' AND id <> '$uid'");
        if($nums>0){
            admin_msg('该支付方式已经存在');
        } else {
            if(!file_exists(MVMMALL_ROOT.'./include/payment/'.$name.'.class.php')){
                admin_msg('该支付方式不存在');
            } else {
                require_once  MVMMALL_ROOT.'./include/payment/'.$name.'.class.php';
            }
            $config = array();
            foreach ($payment[$name]['cfg'] AS $key => $val)
            {
                $config[$key]['name']   = $val['name'];
                $config[$key]['value']  = $$val['name'];
                $config[$key]['label']  = $val['label'];
            }
            $config = serialize($config);
            $query = "UPDATE `{$tablepre}payment_table` SET
                  class_name  = '$name',
                  name        =  '$pay_name',
                  pay_desc    =  '$pay_desc',
                  online      = '$payment[$name][online]',
                  cfg         = '$config'
                  WHERE id    = '$uid' ";
            $db->query($query);
        }
        admin_msg('success','admincp.php?module=payment&action=list');
    } else {
        $pay        = $db->get_one("SELECT * FROM `{$tablepre}payment_table` WHERE id='$uid'");
        $pay['cfg'] = unserialize($pay['cfg']);
        $fields     = array();
        foreach ($pay['cfg'] AS $key => $val)
        {
            $fields[$key]['name']   = $val['name'];
            $fields[$key]['value']  = $val['value'];
            $fields[$key]['label']  = $val['label'];
        }
        $name   = $pay['name'];
        $desc   = $pay['pay_desc'];
        require template('payment_add');
        footer();
    }
} elseif ($action=='del' && is_numeric($uid)){
    $db->query("DELETE FROM `{$tablepre}payment_table` WHERE id='$uid'");
    payment_list('update');
    admin_msg('success','admincp.php?module=payment&action=list');
} else {
    admin_msg('pass_worng');
}